<template>
  <div>
    <div class="top">
      <div style="margin-top: 20px">
        <!-- 分类管理 -->
        <template>
          <div class="block">
            <span class="demonstration">日期</span>
            <el-date-picker
              class="rqk"
              v-model="value1"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </div>
        </template>
      </div>

      <div style="margin-top: 20px">
        <el-form :inline="true" :model="params" class="demo-form-inline">
          <el-form-item label="搜索" style="margin-left: 50px">
            <el-input
              v-model="params.title"
              placeholder="按标题查找"
            ></el-input>
          </el-form-item>

          <el-button
            type="primary"
            icon="el-icon-search"
            style="margin-left: 10px"
            @click="getList(1)"
            >查询</el-button
          >

          <el-button
            type="danger"
            icon="el-icon-plus"
            style="margin-left: 10px"
            @click="add"
            >增加</el-button
          >

            <el-button
            type="warning"
            icon="el-icon-delete"
            style="margin-left: 10px"
            @click="removeMore"
            >批量删除</el-button
          >
        </el-form>
      </div>
    </div>
    <div class="bg">
      <el-table :data="rows" style="width: 100%" @selection-change="handleSelectionChange">
        <!-- <el-table-column label="选中">
          <template slot-scope="scope">
         
            <el-checkbox v-model="scope.row.checked"></el-checkbox>
          </template>
        </el-table-column> -->

          <el-table-column
      type="selection"
      width="55">
    </el-table-column>


        <el-table-column label="图片" prop="img">
          <template slot-scope="scope">
          <img style="width:40px;height:40px"  v-if="scope.row.img" :src="scope.row.img" />
          <span v-else>+</span>
          </template>
        </el-table-column>

        <el-table-column label="跳转地址" prop="url"> </el-table-column>

        <el-table-column label="标题" prop="title"> </el-table-column>

        <el-table-column label="是否启用" prop="status">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="statusChange(scope.row)"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button 
            size="mini" 
            type="primary"
            icon="el-icon-edit-outline"
            @click="Edit(scope.row)"
              >编辑</el-button
            >
            <el-popconfirm
              title="确定删除此数据吗？"
              @confirm="remove(scope.row)"
            >
              <el-button
              style="margin-left:5px" 
              slot="reference"
              size="mini"
              type="danger"
              icon="el-icon-delete"
              >删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="button">
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="getList"
          :current-page="currentPage"
          :page-sizes="[5,10]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <CategoryEdit @load="getList" ref="categoryEditer"></CategoryEdit>
  </div>
</template>



<script>
import CategoryEdit from './categoryEdit'
export default {
  name: "shouyeCategory",
  components: {
    CategoryEdit
  },
  data() {
    return {
      //查询参数
      params: {
        title: "",
        type: "category",
        pageNum: 1,
        pageSize: 5,
      },

      currentPage: 1,
      rows: [],
      value1: "",
      value2: "",
      dialogFormVisible: false,
      total: 0,
      selecteds:[]
    };
  },
  methods: {
    add(){
      this.$refs.categoryEditer.add()
    },
    Edit(row){
      this.$refs.categoryEditer.Edit(row)
    },
    getList(pageNum) {
      if (pageNum) this.params.pageNum = pageNum;
      this.$http({
        url: "/admin/index",
        params: this.params,
      }).then((res) => {
        this.total = res.total;
        this.rows = res.rows;
      });
    },

    handleSelectionChange(rows){
      //存储选中数据
      this.selecteds = rows
    },
    //删除
    remove(row) {
                this.$http({
            url: "/admin/index",
            method: "DELETE",
            params:{indexId:row._id}
          }).then((res) => {
            if (!res.errorMsg) {
              this.getList(1);
            }
          });
    },
    //批量删除
    removeMore(){
      //获取选中的数据
      let checkedRowsPrmose = []
      let success = [],fail = []
      for(let i = 0;i<this.selecteds.length;i++){
        checkedRowsPrmose.push(
          new Promise(resovle => {
            let row = this.selecteds[i]
            this.$http({
            url: "/admin/index",
            method: "DELETE",
            params:{indexId:row._id}
          }).then((res) => {
            if (!res.errorMsg) {
              success.push(row._id)
            }else{
              fail.push(row._id)
            }
            resovle()
          });
          })
        )
      }
      this.$confirm('是否删除数据?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
              Promise.all(checkedRowsPrmose).then(() => {
        this.$message({
          message:`批量删除完成，成功删除${success.length}条数据`,
          duration:3000
        });
        this.getList(1)
      })
        })
      //删除
 
    },
    //修改pagesize
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.getList(1);
    },

//启用停用
    statusChange(row) {
      this.$http({
        url: "/admin/index",
        method: "PUT",
        data: {
          indexId: row._id,
          status: row.status,
        },
      }).then((res) => {
        if (!res.errorMsg) {
          this.getList();
        }
      });
    },
  },
  created() {
    this.getList();
  },
  mounted() {},
};
</script>

<style>
.top {
  display: flex;
  width: 90%;

  margin-left: 50px;
  margin-top: 20px;
  background: #fff;
  /* background: red; */
}
.rqk {
  margin-left: 20px;
}

.bg {
  width: 90%;
  margin-top: 10px;
  margin-left: 50px;
}
.button {
  width: 90%;
  margin-top: 20px;
  margin-left: 50px;
}
</style>
